#include "viewmanager.h"

ViewManager::ViewManager(QWidget *parent, PageWidget *page, IPluginGadget * pluginconfig) :
    QWidget(parent),
    m_pagewidget(page),
    m_view(NULL),
    m_splitter(NULL)
{
    m_view = new ViewWidget(this, m_pagewidget, pluginconfig);
    m_layout = new QHBoxLayout(this);
    m_layout->setMargin(0);
    m_layout->setSpacing(0);
    m_layout->addWidget(m_view);
}

ViewManager::~ViewManager()
{
    if(m_view) {
        delete m_view;
        m_view = 0;
    }
    if(m_splitter) {
        delete m_splitter;
        m_splitter = 0;
    }
}

void ViewManager::saveSettings(QSettings* qs)
{
    if(!qs)
        return;
    if(m_view){
        qs->setValue("type", "plugin");
        qs->setValue("classId", m_view->classId());
        qs->beginGroup("Configuration");
        m_view->getPluginGadget()->saveState(qs);
        qs->endGroup();
    }else{
        qs->setValue("type", "splitter");
        qs->setValue("splitterOrientation", (qint32)m_splitter->orientation());
        QList<QVariant> sizesQVariant;
        foreach (int value, m_splitter->sizes()) {
            sizesQVariant.append(value);
        }
        qs->setValue("splitterSizes", sizesQVariant);
        //first ViewManager
        qs->beginGroup("side0");
        static_cast<ViewManager*>(m_splitter->widget(0))->saveSettings(qs);
        qs->endGroup();
        //second ViewManager
        qs->beginGroup("side1");
        static_cast<ViewManager*>(m_splitter->widget(1))->saveSettings(qs);
        qs->endGroup();
    }
}

void ViewManager::readSettings(QSettings* qs)
{
    if(!qs)
        return;
    QString mode = qs->value("type").toString();
    if(mode == "plugin"){//restore plugin
        QString classId = qs->value("classId").toString();
        int index = m_view->indexOfClassId(classId);
        m_view->switch_plugin(index);
        if(qs->childGroups().contains("Configuration")) {
            qs->beginGroup("Configuration");
            m_view->getPluginGadget()->restoreState(qs);
            qs->endGroup();
        }
    }else{//restore splitter
        qint32 orientation = qs->value("splitterOrientation").toInt();
        QList<QVariant> sizesQVariant = qs->value("splitterSizes").toList();
        m_sizes.clear();
        foreach (QVariant value, sizesQVariant) {
            m_sizes.append(value.toInt());
        }
        split((Qt::Orientation)orientation, true);
        m_splitter->setSizes(m_sizes);
        qs->beginGroup("side0");
        static_cast<ViewManager*>(m_splitter->widget(0))->readSettings(qs);
        qs->endGroup();
        qs->beginGroup("side1");
        static_cast<ViewManager*>(m_splitter->widget(1))->readSettings(qs);
        qs->endGroup();
    }
}

void ViewManager::split(Qt::Orientation orientation, bool restoring)
{
    m_splitter = new QSplitter(this);
    m_splitter->setOrientation(orientation);
    m_layout->removeWidget(m_view);
    m_layout->addWidget(m_splitter);
    if(restoring){//initial restoring handler
        m_splitter->addWidget(new ViewManager(this, m_pagewidget));
        m_splitter->addWidget(new ViewManager(this, m_pagewidget));
    }else{//normal split handler,remain old plugin
        IPluginGadget * plugingadget = m_view->getPluginGadget();
        m_view->removePlugin();
        m_splitter->addWidget(new ViewManager(this, m_pagewidget, plugingadget));
        m_splitter->addWidget(new ViewManager(this, m_pagewidget));
        //get a new plugin focused,must be done
    }
    if(m_view->isFocus())
        m_pagewidget->setCurrentPlugin(static_cast<ViewManager*>(m_splitter->widget(0))->view()->getPluginGadget());

    delete m_view;
    m_view = 0;
}

void ViewManager::unsplit(ViewManager *child2remove)
{
    if (!m_splitter)
        return;
    //Delete this child that needed to delete.
    child2remove->hide();
    delete child2remove;
    //Find another child
    ViewManager *anotherchild = qobject_cast<ViewManager*>(m_splitter->widget(0));
    QSplitter *oldSplitter = m_splitter;
    //Check another child,handle it view or splitter
    if (anotherchild->isSplitter()) {
        m_splitter = anotherchild->takeSplitter();
        m_splitter->setParent(this);
        m_layout->addWidget(m_splitter);
        m_view = 0;
    } else {
        m_splitter = 0;
        m_view = anotherchild->takeView();
        m_view->setParent(this);
        m_layout->addWidget(m_view);
    }
    m_layout->removeWidget(oldSplitter);
    delete oldSplitter;
    m_pagewidget->setCurrentPlugin(findFirstView()->view()->getPluginGadget());
}

QSplitter* ViewManager::takeSplitter()
{
    QSplitter *oldSplitter = m_splitter;
    if (m_splitter)
        m_layout->removeWidget(m_splitter);
    m_splitter->setParent(0);
    m_splitter = 0;
    return oldSplitter;
}

ViewWidget* ViewManager::takeView()
{
    ViewWidget *oldView = m_view;
    if (m_view)
        m_layout->removeWidget(m_view);
    m_view->setParent(0);
    m_view = 0;
    return oldView;

}

void ViewManager::mousePressEvent(QMouseEvent *e)
{
    if (e->button() != Qt::LeftButton)
        return;
    if(m_view){
        this->setFocus(Qt::MouseFocusReason);//focus this viewmanager
        m_pagewidget->setCurrentPlugin(m_view->getPluginGadget());
    }
}

ViewManager* ViewManager::findFirstView()
{
    if (m_splitter) {
        for (int i = 0; i < m_splitter->count(); ++i) {
            if (ViewManager *next = qobject_cast<ViewManager*>(m_splitter->widget(i)))
                if (ViewManager *result = next->findFirstView())
                    return result;
        }
        return 0;
    }
    return this;
}

ViewManager* ViewManager::findViewManager(IPluginGadget *pluginconfig)
{
    if (m_view && (m_view->getPluginGadget() == pluginconfig))
        return this;
    if (m_splitter) {
        int count = m_splitter->count();
        for (int i = 0; i < count; i++) {
            if (ViewManager *next = qobject_cast<ViewManager*>(m_splitter->widget(i)))
                if (ViewManager *result = next->findViewManager(pluginconfig))
                    return result;
        }
    }
    return 0;
}

ViewManager* ViewManager::findViewManager(ViewWidget *view)
{
    if (m_view == view)
        return this;
    if (m_splitter) {
        int count = m_splitter->count();
        for (int i = 0; i < count; i++) {
            if (ViewManager *next = qobject_cast<ViewManager*>(m_splitter->widget(i)))
                if (ViewManager *result = next->findViewManager(view))
                    return result;
        }
    }
    return 0;
}


ViewManager* ViewManager::findViewManager(ViewManager *child)
{
    if (m_splitter) {
        int count = m_splitter->count();
        for (int i = 0; i < count; i++) {
            if (ViewManager *next = qobject_cast<ViewManager*>(m_splitter->widget(i))){
                if (next == child)
                    return this;
                if (ViewManager *result = next->findViewManager(child))
                    return result;
            }
        }
    }
    return 0;
}
